/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.statementView;
/**
 * 说明：
 * 资金出入
 * @Author：黄盛强
 * @Date：2012-03-20
 */
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.RowSorterEvent;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;

import com.ruipengkj.ApplicationContext;
import com.ruipengkj.bean.TMoneyInOut;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.base.query.PageIndex;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.commons.base.query.PageView;
import com.ruipengkj.commons.util.TabelUtil;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.JLabelJComboBoxComponent;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.component.jpanel.RPJPanelStatementTop;
import com.ruipengkj.component.jtable.RPTable;
import com.ruipengkj.component.jtable.TableRowSorter;
import com.ruipengkj.component.page.PagingComponent;
import com.ruipengkj.model.MoneyInOntModel;
import com.ruipengkj.service.TMoneyInOutService;
import com.ruipengkj.view.base.BasePagePanel;
import com.ruipengkj.view.memberView.MemberFillCardPanel;
import com.ruipengkj.view.orderView.MarketReturnedAddOrModifyDialog;
import com.ruipengkj.view.orderView.OrderDetailDialog;
import com.ruipengkj.view.purchaseView.PurchaseEnterAddOrModifyPanel;
import com.ruipengkj.view.purchaseView.PurchaseRetreatAddPanel;
import com.ruipengkj.view.reserveView.ArticlesUsingAddDialog;
import com.ruipengkj.view.reserveView.CommodityDamageAddDialog;
import com.ruipengkj.view.reserveView.OtherReserveAddDialog;

public class MarketMoneyInOntPanel extends BasePagePanel<TMoneyInOut> implements Runnable {

	private static final long serialVersionUID = 1L;
	
	private JLabelJComboBoxComponent nameID;//编号/经手人
	
	private JTextField nameID_tf;
	
	private JLabelJComboBoxComponent filte;//过滤
	
	private JButton queryButton;//查询
	
	private JTable table;
	private JScrollPane tableJsp;
	private MoneyInOntModel moneyInOntModel;
	
	private PagingComponent pagingComponent;//翻页组件
	private PageView<TMoneyInOut> pageView;//翻页主键
	
	private TMoneyInOutService tMoneyInOutService;//资金出入
	
	public MarketMoneyInOntPanel() {
		tMoneyInOutService=BuildSpringApplication.getInstance().getTMoneyInOutServiceImpl();
		this.setLayout(new BorderLayout());
		this.init();
		this.initListener();
		this.setBackground(Util.PANEL_COLOR);
	}
	
	//产生一个普通表格
	public RPTable createRPJTable(AbstractTableModel model,MouseMotionListener mm,MouseListener ml,int rowHeight,int align){
		RPTable table=new RPTable(model) {
			@Override
			public void sorterChanged(RowSorterEvent e) {e = null;
				e.getPreviousRowCount();
				System.out.println("ttttttttttttttt");
				super.sorterChanged(e);
			}
		};
		table.addMouseMotionListener(mm);
		table.addMouseListener(ml);
		table.setRowHeight(rowHeight);
		table.getTableHeader().setReorderingAllowed(false); 
		//表格剧中
		DefaultTableCellRenderer   defaultTableCellRenderer   =   new   DefaultTableCellRenderer();   
		defaultTableCellRenderer.setHorizontalAlignment(align);   
		table.setDefaultRenderer(Object.class,defaultTableCellRenderer);
		return table;
	}
		
	private void init() {
		pageView = new PageView<TMoneyInOut>();
		
		moneyInOntModel = new MoneyInOntModel();
		table=createRPJTable(moneyInOntModel, null, this, 30, JLabel.CENTER);
//		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		tableJsp=new JScrollPane(table);
		tableJsp.setOpaque(false);
		pagingComponent=new PagingComponent();
		pagingComponent.setPageDelegale(this);
		
		this.setOpaque(true);
		this.setBackground(new Color(247, 252, 255));
		table.setOpaque(false);
		tableJsp.setOpaque(false);
		tableJsp.getViewport().setOpaque(false); 
		this.add(uiFactory.createFlowJPanel(), BorderLayout.WEST);
		this.add(uiFactory.createFlowJPanel(), BorderLayout.EAST);
		
		this.add(initTop(), BorderLayout.NORTH);
		this.add(tableJsp, BorderLayout.CENTER);
		this.add(tailPanel(), BorderLayout.SOUTH);
		
		// 排序
		TableRowSorter<MoneyInOntModel> tableRowSorter = new TableRowSorter<MoneyInOntModel>(
				moneyInOntModel);
		table.setRowSorter(tableRowSorter);
//		SwingUtilities.invokeLater(statementStockPanel);
	}
	
	private JPanel tailPanel() {
		JPanel tail_panel = uiFactory.createBorderJPanel();
		
		JPanel jp = uiFactory.createGridJPanel(1, 2);
		JPanel jp2 = uiFactory.createFlowJPanel(FlowLayout.LEFT);
		jp2.add(uiFactory.createJLabel("       "));
		
		pagingComponent=new PagingComponent();
		pagingComponent.setPageDelegale(this);
		Util.setSize(pagingComponent, 80, 24);
		jp.add(jp2);
		jp.add(pagingComponent);
		
		tail_panel.add(jp);
		tail_panel.add(uiFactory.createFlowJPanel(), BorderLayout.NORTH);
		tail_panel.add(uiFactory.createFlowJPanel(), BorderLayout.SOUTH);
		return tail_panel;
	}
	
	private JPanel initTop() {
		JPanel top_panel = new RPJPanelStatementTop();
		filte = new JLabelJComboBoxComponent("", ApplicationContext.MONEYINOUT_TYPE_MAP(),188);
		nameID = new JLabelJComboBoxComponent("", new String[]{"编号：", "经手人："},188);
		nameID_tf = new JTextField("", 30);
		queryButton = new RPJButtonBlue("查询");
		top_panel.add(filte);
		top_panel.add(nameID);
		top_panel.add(nameID_tf);
		top_panel.add(queryButton);
		top_panel.setBackground(Util.PANEL_COLOR);
		return top_panel;
	}
	
	private void initListener() {
		queryButton.addActionListener(this);
		filte.getBox().addActionListener(this);
		nameID_tf.addKeyListener(this);
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == queryButton) {
			this.termQuery(1);
		}else if(e.getSource()==filte.getBox()){
			//资金出入类型查询
			this.termQuery(1);
		}
	}
	@Override
	public void keyPressed(KeyEvent e) {
		if(e.getSource()==nameID_tf||e.getSource()==nameID_tf){
			//查询
			if(e.getKeyCode()==KeyEvent.VK_ENTER){
				queryButton.doClick();
			}
		}
	}
	@Override
	public void run() {
		this.termQuery(1);
	}
	@Override
	protected PageView<TMoneyInOut> getPageView() {
		return pageView;
	}

	@Override
	//条件查询
	protected void termQuery(final int currentPage){
		final TMoneyInOut moneyInOut=new TMoneyInOut();
		moneyInOut.setType((Byte)filte.getKeyByVaule(filte.getBox().getSelectedItem().toString()));
		String temp=nameID.getBox().getSelectedItem().toString();
		if(temp.equals("编号：")){
			moneyInOut.setInOutID(nameID_tf.getText().trim());
		}else{
			moneyInOut.setOperator(nameID_tf.getText().trim());
		}
		new Thread(){
			public void run(){
				tableJsp.getViewport().remove(table);
				tableJsp.getViewport().add(waitComponent);
				try {
					pageView.setCurrentPage(currentPage);
					PageModel<TMoneyInOut> pageModel = tMoneyInOutService.getScrollService(pageView.getFirstResult(), pageView.getMaxResult(),moneyInOut);
					pageView.setPageModel(pageModel);
					pageView.setPageIndex(PageIndex.getPageIndex(pageView.getPageCode(), pageView.getCurrentPage(), pageView.getTotalPage()));
					if(pageModel!=null){
						TabelUtil.clearTableRowSorter(table);
						moneyInOntModel.setRowData(pageModel.getResultList());
						SwingUtilities.invokeLater(new Runnable() {
							@Override
							public void run() {
								tableJsp.getViewport().remove(waitComponent);
								tableJsp.getViewport().add(table);
								TabelUtil.setJTabelAutoClumnWidth(table);
								pagingComponent.show();
							}
						});
					}else{
						//没有订单数据
						tableJsp.getViewport().remove(waitComponent);
						tableJsp.getViewport().add(table);
						moneyInOntModel.clear();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}
	
	//出入原因类型TINYINT  1为采购引起的出款 2为采购退货引起收款
	//3为销售引起的收款 4为销售退货引起的出款 5为充值收款 10为费用单 
	@Override
	public void mouseClicked(MouseEvent e) {
		if (e.getClickCount() == 2 && e.getSource() == table) {
			String source = (String) table.getValueAt(table.getSelectedRow(), 4);
			switch (ApplicationContext.MONEYINOUT_SOURCE_TYPE((String)table.getValueAt(table.getSelectedRow(), 3))) {
				case 1:
					PurchaseEnterAddOrModifyPanel purchaseEnterAddPanel = new PurchaseEnterAddOrModifyPanel(null, "采购单详细", true);
					purchaseEnterAddPanel.showDialog(source);
					break;
				case 2:
					PurchaseRetreatAddPanel purchaseRetreatAddPanel = new PurchaseRetreatAddPanel(null, "采购退货单详细", true);
					purchaseRetreatAddPanel.showDialog(source);
					break;
				case 3:
					OrderDetailDialog orderDetailDialog = new OrderDetailDialog(null, "销售详细", true);
					orderDetailDialog.showDialog(source);
					break;
				case 4:
					MarketReturnedAddOrModifyDialog marketReturnedAddOrModifyDialog = new MarketReturnedAddOrModifyDialog(null,"销售退货详情",true);
					marketReturnedAddOrModifyDialog.showDialog(source);
					break;
				case 5:
					new MemberFillCardPanel().showDialog(source);
					break;
				case 6:
					new CommodityDamageAddDialog(null, "商品报损详情", true).showDialog(source);
					break;
				case 7:
					new ArticlesUsingAddDialog(null, "物品领用详情", true).showDialog(source);
					break;
				case 8:
					new OtherReserveAddDialog(null, "其他方式出库单详情", true).showDialog(source);
					break;
				case 10:
					OtherReserveAddDialog OtherReserveAddDialog = new OtherReserveAddDialog(null, "其他方式出库单详情", true);
					OtherReserveAddDialog.showDialog(source);
					break;
			}
		}
	}
	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}
	
}
